package com.xiaoran.service.zk;

import com.xiaoran.service.config.ServerValue;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ZKit {
    
    @Autowired
    private ZkClient zkClient;
    @Autowired
    private ServerValue serverValue;
    
    //创建根目录
    public void createRootNode() {
        String zkRoot = serverValue.getZkRoot();
        boolean root = zkClient.exists(zkRoot);
        if (!root){
            zkClient.createPersistent(zkRoot);
        }
    }
    
    //创建持久服务节点
    public void createPersistentNode(String path) {
        String pathName=serverValue.getZkRoot()+"/"+path;
        boolean exists = zkClient.exists(pathName);
        if (!exists){
            zkClient.createPersistent(pathName);
        }
    }
    //创建节点
    public void createNode(String path) {
        String pathName=serverValue.getZkRoot()+"/"+path;
        boolean exists = zkClient.exists(pathName);
        if (!exists){
            zkClient.createEphemeral(pathName);
        }
    }
}
